package com.example.main.rottweiljson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.main.rottweiljson.bean.JsonCommonFieldFormat;
import com.example.main.rottweiljson.bean.JsonFieldDetail;
import com.example.main.rottweiljson.bean.ResultJsonBean;
import com.litesuits.common.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ParseRottweilJsonTempelet {

    public static final String DIR_JSON_TXT = "rottweil_json_format.rottweil";
    public static final String LABEL_URL = "url";
    public static final String LABEL_NAME = "labelName";
    public static final String LABEL_DATE_FORMAT = "labelDateFormat";
    public static final String LABEL_TYPE = "type";
    public static final String LABEL_INDEX = "index";
    public static final String LABEL_ELEMTYPE = "elemType";
    public static final String STRING_SPLIC_SYMBOL_ARRAY = "@;@";
    public static final String STRING_SPLIC_SYMBOL_START = "={";
    public static final String STRING_SPLIC_SYMBOL_END = "}";

    /**
     * 根据模板获得需要解析的json格式
     *
     * @return
     */
    public static JsonCommonFieldFormat getCommonJsonFormat() {
        JsonCommonFieldFormat jsonCommonFieldFormat = new JsonCommonFieldFormat();
        try {
            File file = new File(DIR_JSON_TXT);
            List<String> stringList = FileUtils.readLines(file);
            for (int i = 0; i < stringList.size(); i++) {
                String lineContent = stringList.get(i);
                if (lineContent.startsWith(LABEL_URL)) {
                    String url = getValue(lineContent);
                    jsonCommonFieldFormat.setTempleUrl(url);
                    boolean isLoop = true;
                    while (isLoop) {
                        if (url.indexOf(STRING_SPLIC_SYMBOL_START) > 0) {
                            String paramName = url.substring(url.indexOf(STRING_SPLIC_SYMBOL_START) + 1, url.indexOf(STRING_SPLIC_SYMBOL_END) + 1);
                            jsonCommonFieldFormat.getParamterName().add(paramName);
                            url = url.substring(url.indexOf(paramName) + paramName.length());
                        } else {
                            isLoop = false;
                        }
                    }
                } else if (lineContent.startsWith(LABEL_DATE_FORMAT)) {
                    jsonCommonFieldFormat.setDateFormat(getValue(lineContent));
                } else if (lineContent.startsWith(LABEL_NAME)) {
                    String[] stringArray = lineContent.split(STRING_SPLIC_SYMBOL_ARRAY);
                    JsonFieldDetail jsonFieldDetail = new JsonFieldDetail();
                    for (int j = 0; j < stringArray.length; j++) {
                        String line = stringArray[j].trim();
                        if (line.startsWith(LABEL_NAME)) {
                            jsonFieldDetail.setLabelName(getValue(line));
                        } else if (line.startsWith(LABEL_TYPE)) {
                            jsonFieldDetail.setType(getValue(line));
                        } else if (line.startsWith(LABEL_INDEX)) {
                            jsonFieldDetail.setIndex(getValue(line));
                        } else if (line.startsWith(LABEL_ELEMTYPE)) {
                            jsonFieldDetail.setElemType(getValue(line));
                        }
                    }
                    jsonCommonFieldFormat.getJsonFieldDetailList().add(jsonFieldDetail);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonCommonFieldFormat;
    }

    /**
     * 获取内容列表
     *
     * @param jsonCommonFieldFormat
     * @param jsonContent
     * @return
     */
    public static List<List<ResultJsonBean>> getContentList(JsonCommonFieldFormat jsonCommonFieldFormat, String jsonContent) {
        List<List<ResultJsonBean>> resultListAll = new ArrayList<>();
        Object o = JSON.parse(jsonContent);
        if (o instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) o;
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                List<ResultJsonBean> resultJsonBeanList = new ArrayList<>();
                for (int k = 0; k < jsonCommonFieldFormat.getJsonFieldDetailList().size(); k++) {
                    String labelName = jsonCommonFieldFormat.getJsonFieldDetailList().get(k).getLabelName();
                    if (jsonObject.containsKey(labelName)) {
                        ResultJsonBean resultJsonBean = new ResultJsonBean();
                        resultJsonBean.setJsonFieldDetail(jsonCommonFieldFormat.getJsonFieldDetailList().get(k));
                        resultJsonBean.setValue(jsonObject.get(labelName));
                        resultJsonBeanList.add(resultJsonBean);
                    }
                }
                resultListAll.add(resultJsonBeanList);
            }
        } else if (o instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) o;
            System.out.println(jsonObject);
        }
        return resultListAll;
    }

    public static String getValue(String value) {
        return value.substring(value.indexOf("=") + 1);
    }
}
